package com.hupu.greap;
//webmagic包
import com.hupu.greap.mysql_db.HupuMysql;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;
//java.net包
import java.net.HttpURLConnection;
import java.net.URL;
//正则表达式包
import java.sql.SQLException;
import java.util.Scanner;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

//输入

public class hupugreap implements PageProcessor {

    //请求头
    private static final String url = "https://voice.hupu.com/nba";
    public static final String user = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 Edg/130.0.0.0";
    public static final String acceptLanguage = "en,zh-CN;q=0.9,zh-TW;q=0.8,zh;q=0.7";


    //预请求代码
    public static boolean requested() {
        HttpURLConnection connection;
        try {
            URL urlObj = new URL(url);
            connection = (HttpURLConnection) urlObj.openConnection();
            connection.setRequestProperty("User-Agent", user);
            connection.setRequestProperty("Accept-Language", acceptLanguage);
            int responseCode = connection.getResponseCode();
            System.out.println("Response Code: " + responseCode);
            if (responseCode == 200) {
                System.out.println("请求成功");
                return true;
            } else {
                System.out.println("请求失败，状态码：" + responseCode);
                return false;
            }
        } catch (Exception e) {
            System.out.println("请求过程中发生异常：" + e);
            return false;
        }
    }

    //处理字符没用的字符
    public String gettxt(String get)
    {
        String regex="<.*?>";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(get);
        String result=matcher.replaceAll("");
        System.out.println(result);
        return result;
    }
    //用户输入
    public static int getUserInput() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入爬取的页数：");
        int pageNum = scanner.nextInt();
        if (pageNum <= 0)
        {
            System.out.println("页数不能小于1，请重新输入！");
            return getUserInput();

        }
        return pageNum;
    }


    //初始化程序
    private Site site = Site.me()
            .setRetryTimes(3)
            .setSleepTime(1000);



    public void process(Page page) {

        String title = page.getHtml().xpath("//*[@id='__next']/div/header/div/h1").toString();
        gettxt("标题:"+title);

        String regex = "\\d+";
        String xpath_c;
        String xpath_t;
        xpath_c = "//*[@id=__next]/div/section/div/main/a[1]/div/div/p";
        xpath_t = "//*[@id=__next]/div/section/div/main/a[1]/div/div/span/time";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher_c = pattern.matcher(xpath_c);
        Matcher matcher_t = pattern.matcher(xpath_t);
        int i = 0;
        String result_center_path = null;
        String result_time_path = null;
//        获取页数
        int pageNum = getUserInput();
        while (i < pageNum) {
            i++;
            try {
                Thread.sleep(2000);
                result_center_path = matcher_c.replaceAll(String.valueOf(i));
                String center = page.getHtml().xpath(result_center_path).toString();
                String getcenter = gettxt(center);
                result_time_path = matcher_t.replaceAll(String.valueOf(i));
                String timer = page.getHtml().xpath(result_time_path).toString();
                String gettime = gettxt(timer);
                HupuMysql hupumysql = new HupuMysql();
                hupumysql.insert(getcenter,gettime);
                System.out.println("----------------------------------------------");
            } catch (SQLException | InterruptedException e) {
                throw new RuntimeException(e);
            }

        }


    }

    @Override
    public Site getSite() {
        return site;
    }

    public static void main(String[] args) {
        System.out.println("爬虫已启动");

        boolean requsted = requested();
        if (requsted) {
            Spider.create(new hupugreap())
                    .addUrl(url)
                    .thread(5)
                    .run();
        } else {
            System.out.println("由于网络问题或URL不正确，爬取未启动。请检查URL的合法性，并在网络稳定的情况下适当重试。");
        }
    }
}